package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: health_parent
 * @description: 检查组
 * @author: HuChunping
 * @create: 2021-01-08 19:54
 **/

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    /**
    * @Description: 添加检查组
    * @Param: [checkitemIds：选中的检查项的id数组,  checkGroup：检查组信息]
    * @return: com.itheima.health.entity.Result
    * @Author: HuChunping
    * @Date: 2021/1/8-20:05
    */
    // 参数没加requestparam注解，形参必须和前端传来的一样
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkgroup, Integer[] checkitemIds){
        checkGroupService.add(checkitemIds, checkgroup);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
    * @Description: 分页查询检查组
    * @Param: [queryPageBean]
    * @return: com.itheima.health.entity.Result
    * @Author: HuChunping
    * @Date: 2021/1/9-17:48
    */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult<CheckGroup> pageResult = checkGroupService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, pageResult);
    }

    /**
    * @Description: 通过id查询检查组
    * @Param: [id]
    * @return: com.itheima.health.entity.Result
    * @Author: HuChunping
    * @Date: 2021/1/9-17:48
    */
    @GetMapping("/findById")
    public Result findById(int id){
        CheckGroup checkGroup = checkGroupService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
    }

    /**
    * @Description:  通过检查组id查询中间表获取勾选的检查项
    * @Param: [id]
    * @return: com.itheima.health.entity.Result
    * @Author: HuChunping
    * @Date: 2021/1/9-17:48
    */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(int id){
        List<Integer> checkitemIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkitemIds);
    }

    /**
    * @Description: 编辑检查组
    * @Param: [checkgroup, checkitemIds]
    * @return: com.itheima.health.entity.Result
    * @Author: HuChunping
    * @Date: 2021/1/9-19:54
    */
    @PostMapping("/update")
    public Result update(@RequestBody CheckGroup checkgroup, Integer[] checkitemIds){
        checkGroupService.update(checkitemIds, checkgroup);
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
    * @Description: 删除检查组
    * @Param: [id]
    * @return: com.itheima.health.entity.Result
    * @Author: HuChunping
    * @Date: 2021/1/9-19:54
    */
    @PostMapping("/deleteById")
    public Result deleteById(int id){
        checkGroupService.deleteById(id);
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
    * @Description: 查询所有检查组
    * @Param: []
    * @return: com.itheima.health.entity.Result
    * @Author: HuChunping
    * @Date: 2021/1/11-14:45
    */
    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> checkGroups = checkGroupService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroups);
    }
}
